<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>双指针算法：合并两个有序数组</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
        }
        .code-block {
            background: #1e293b;
            border-radius: 8px;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .card {
            transition: all 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            background: linear-gradient(90deg, rgba(236, 253, 245, 0.5), rgba(236, 253, 245, 0.3));
            border-left: 4px solid #10b981;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl flex flex-col items-center text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">双指针算法精解</h1>
            <h2 class="text-2xl md:text-3xl font-medium mb-8">合并两个有序数组</h2>
            <p class="text-lg md:text-xl max-w-2xl opacity-90">一种高效处理有序数据结构的经典算法范式</p>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-4xl px-4 md:px-0 py-12">
        <!-- Problem Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-500 mr-3 rounded-full"></div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 font-serif">题目描述</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 card">
                <p class="text-gray-700 mb-4">给定两个按非递减顺序排列的整数数组 <code class="px-2 py-1 bg-gray-100 rounded text-indigo-600">nums1</code> 和 <code class="px-2 py-1 bg-gray-100 rounded text-indigo-600">nums2</code>，请你将 <code class="px-2 py-1 bg-gray-100 rounded text-indigo-600">nums2</code> 合并到 <code class="px-2 py-1 bg-gray-100 rounded text-indigo-600">nums1</code> 中，使 <code class="px-2 py-1 bg-gray-100 rounded text-indigo-600">nums1</code> 成为一个按非递减顺序排列的数组。</p>
                <div class="highlight p-4 rounded-lg my-4">
                    <p class="text-gray-700"><i class="fas fa-info-circle text-emerald-500 mr-2"></i>请注意，最终合并后数组不应由函数返回，而是存储在 <code class="px-1 py-0.5 bg-emerald-100 rounded text-emerald-700">nums1</code> 中。</p>
                </div>
            </div>
        </section>

        <!-- Key Points Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-500 mr-3 rounded-full"></div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 font-serif">核心考点</h2>
            </div>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-bullseye text-indigo-500 text-lg"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">双指针技巧</h3>
                    </div>
                    <p class="text-gray-700">通过两个指针分别遍历数组，高效处理有序数据的合并操作</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                            <i class="fas fa-clock text-blue-500 text-lg"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">时间复杂度</h3>
                    </div>
                    <p class="text-gray-700">O(n + m) - 只需遍历两个数组各一次</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-emerald-100 flex items-center justify-center mr-4">
                            <i class="fas fa-memory text-emerald-500 text-lg"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">空间复杂度</h3>
                    </div>
                    <p class="text-gray-700">O(1) - 原地合并，无需额外空间</p>
                </div>
            </div>
        </section>

        <!-- Solution Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-500 mr-3 rounded-full"></div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 font-serif">解题思路</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 mb-8 card">
                <div class="flex items-start">
                    <div class="text-indigo-500 text-2xl mr-4 mt-1">1</div>
                    <div>
                        <h3 class="text-xl font-bold text-gray-800 mb-3">从末尾开始比较</h3>
                        <p class="text-gray-700">从两个数组的末尾开始比较，将较大的元素放入结果数组的末尾，逐步向前移动指针。</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 card">
                <div class="flex items-start">
                    <div class="text-indigo-500 text-2xl mr-4 mt-1">2</div>
                    <div>
                        <h3 class="text-xl font-bold text-gray-800 mb-3">处理剩余元素</h3>
                        <p class="text-gray-700">如果 nums2 中还有剩余元素，直接复制到 nums1 的前面。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-500 mr-3 rounded-full"></div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 font-serif">算法可视化</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="mermaid">
                    %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#6366f1', 'primaryBorderColor': '#4f46e5', 'primaryTextColor': '#ffffff'}}}%%
                    graph LR
                    A[初始化指针] --> B[比较元素]
                    B --> C{哪个更大?}
                    C -->|nums1[p1] > nums2[p2]| D[放入nums1[p1]]
                    C -->|nums1[p1] ≤ nums2[p2]| E[放入nums2[p2]]
                    D --> F[移动p1和p指针]
                    E --> G[移动p2和p指针]
                    F --> H{还有元素未处理?}
                    G --> H
                    H -->|是| B
                    H -->|否| I[检查nums2剩余]
                    I --> J{还有nums2元素?}
                    J -->|是| K[复制剩余元素]
                    J -->|否| L[完成]
                </div>
            </div>
        </section>

        <!-- Code Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-500 mr-3 rounded-full"></div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 font-serif">示例代码</h2>
            </div>
            <div class="code-block p-6 rounded-xl">
                <div class="flex justify-between items-center mb-4">
                    <div class="flex space-x-2">
                        <div class="w-3 h-3 rounded-full bg-red-500"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <span class="text-gray-400 text-sm">Python</span>
                </div>
                <pre class="text-gray-300 overflow-x-auto"><code>def merge(nums1, m, nums2, n):
    <span class="text-blue-400"># 初始化三个指针</span>
    p1, p2, p = m - 1, n - 1, m + n - 1
    
    <span class="text-blue-400"># 从后向前比较并合并</span>
    <span class="text-yellow-400">while</span> p2 >= <span class="text-green-400">0</span> <span class="text-yellow-400">and</span> p1 >= <span class="text-green-400">0</span>:
        <span class="text-yellow-400">if</span> nums1[p1] > nums2[p2]:
            nums1[p] = nums1[p1]
            p1 -= <span class="text-green-400">1</span>
        <span class="text-yellow-400">else</span>:
            nums1[p] = nums2[p2]
            p2 -= <span class="text-green-400">1</span>
        p -= <span class="text-green-400">1</span>
    
    <span class="text-blue-400"># 处理nums2中剩余元素</span>
    <span class="text-yellow-400">while</span> p2 >= <span class="text-green-400">0</span>:
        nums1[p] = nums2[p2]
        p2 -= <span class="text-green-400">1</span>
        p -= <span class="text-green-400">1</span></code></pre>
            </div>
        </section>

        <!-- Key Concepts Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-500 mr-3 rounded-full"></div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 font-serif">关键概念</h2>
            </div>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-arrows-alt-h text-indigo-500 mr-3"></i>双指针技术
                    </h3>
                    <p class="text-gray-700">双指针是一种常见的算法优化技术，特别适用于有序数组或链表的操作。通过使用两个指针分别指向不同位置，可以高效地解决合并、查找、比较等问题。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-sort-amount-down-alt text-indigo-500 mr-3"></i>原地算法
                    </h3>
                    <p class="text-gray-700">原地算法是指在处理数据时，不需要额外的存储空间，直接在输入数据上进行修改。这种算法可以显著减少内存使用，但需要谨慎处理数据覆盖问题。</p>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: '"Noto Sans SC", sans-serif'
        });
    </script>
</body>
</html>